package com.maple.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.maple.constant.PasswordConstant;
import com.maple.constant.StatusConstant;
import com.maple.dto.EmployeeDTO;
import com.maple.dto.EmployeeLoginDTO;
import com.maple.dto.EmployeePageQueryDTO;
import com.maple.dto.EmployeePasswordDTO;
import com.maple.entity.Employee;
import com.maple.enumeration.ExceptionEnum;
import com.maple.exception.BusinessException;
import com.maple.mapper.EmployeeMapper;
import com.maple.result.PageResult;
import com.maple.service.EmployeeService;
import com.maple.vo.EmployeeLoginVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;


    /**
     * 用户登陆
     */
    @Override
    public EmployeeLoginVO login(EmployeeLoginDTO employeeLoginDTO) {
        // 获取数据库中的用户
        Employee employee = employeeMapper.getByUsername(employeeLoginDTO.getUsername());

//        判断用户是否存在
        if(employee == null){
            throw new BusinessException(ExceptionEnum.ACCOUNT_NOT_FOUND);
        }

        String password = employeeLoginDTO.getPassword();
//        进行密码加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());


//        校验密码
        if(!password.equals(employee.getPassword())){
            throw new BusinessException(ExceptionEnum.vPASSWORD_ERROR);
        }

//        检查用户状态
        if(employee.getStatus() == StatusConstant.DISABLE){
            throw new BusinessException(ExceptionEnum.ACCOUNT_LOCKED);
        }

        return new EmployeeLoginVO().builder()
                .id(employee.getId())
                .name(employee.getName())
                .userName(employee.getUsername())
                .build();

    }

    /**
     * 新增用户
     */
    @Override
    public void save(EmployeeDTO employeeDTO) {
        // 进行对象拷贝
        Employee employee = new Employee();
        BeanUtils.copyProperties(employeeDTO, employee);

//        密码初始化
        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
//        状态初始化
        employee.setStatus(StatusConstant.ENABLE);

//        数据库新增对象
        employeeMapper.saveEmployee(employee);

    }

    /**
     * 分页查询
     * @param pageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(EmployeePageQueryDTO pageQueryDTO) {
//        开始分页数据查询
        PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
        Page<Employee> page = employeeMapper.pageQuery(pageQueryDTO);

//        封装分页数据对象
        Long total = page.getTotal();
        List<Employee> list = page.getResult();

//        返回数据
        return new PageResult(total, list);
    }

    @Override
    public void empStatus(String status, String id) {
//        封装员工对象
        Employee emp = Employee.builder()
                .id(Long.valueOf(id))
                .status(Integer.valueOf(status))
                .build();
//        通过id修改员工状态
        employeeMapper.updata(emp);
    }

    @Override
    public Employee queryById(String id) {
//        通过id查询员工
        Employee emp = employeeMapper.queryById(id);
        emp.setPassword("***");
//        返回数据
        return emp;
    }

    @Override
    public void update(EmployeeDTO employeeDTO) {
//        封装对象
        Employee emp = new Employee();
        BeanUtils.copyProperties(employeeDTO, emp);

//        数据库更新
        employeeMapper.updata(emp);
    }

    @Override
    public void changePassword(EmployeePasswordDTO employeePasswordDTO) {
//        查询数据库密码
        Employee emp = employeeMapper.queryById(String.valueOf(employeePasswordDTO.getEmpId()));
        String dataPassword = emp.getPassword();

//        校验旧密码是否与数据库中的一样
        String oldPassword = DigestUtils.md5DigestAsHex(employeePasswordDTO.getOdPassword().getBytes());

        if(!dataPassword.equals(oldPassword)){
            throw new BusinessException(ExceptionEnum.OLD_PASSWORD_MISTAKE);
        }

//        修改密码
        String newPassword = DigestUtils.md5DigestAsHex(employeePasswordDTO.getNewPassword().getBytes());
        emp.setPassword(newPassword);
        employeeMapper.updata(emp);
    }


}
